package com.cpi.basilos.silo.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.cpi.basilos.location.service.LocationService;
import com.cpi.basilos.location.view.LocationView;
import com.cpi.basilos.reason.movement.service.ReasonMovementService;
import com.cpi.basilos.reason.movement.view.ReasonMovementView;
import com.cpi.basilos.silo.converter.SiloConverter;
import com.cpi.basilos.silo.converter.SiloMovementsConverter;
import com.cpi.basilos.silo.dao.SiloDAO;
import com.cpi.basilos.silo.dto.SiloEntryDTO;
import com.cpi.basilos.silo.dto.SiloEntryMovementsDTO;
import com.cpi.basilos.silo.dto.SiloFiltersDTO;
import com.cpi.basilos.silo.dto.SiloOutputDTO;
import com.cpi.basilos.silo.exception.SiloException;
import com.cpi.basilos.silo.view.SiloEntityMovementsView;
import com.cpi.basilos.silo.view.SiloEntityView;
import com.cpi.basilos.silo.view.SiloFilterView;
import com.cpi.basilos.silo.view.SiloFiltersView;
import com.cpi.basilos.silo.view.SiloOutputView;
import com.cpi.basilos.unit.exception.UnitException;
import com.cpi.basilos.unit.service.UnitService;
import com.cpi.basilos.unit.view.UnitView;
import com.cs.common.exception.CSException;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.TimeUtils;

public class SiloService {
	private static final String OUTPUT_REASON_CODE = "OUTPUT_REASON_CODE";//$NON-NLS-1$
	private static org.apache.log4j.Logger logger = Logger.getLogger(SiloService.class);
	
	public List getReports(SiloFilterView siloFilterView)throws SiloException{
		List siloEntryMovementsViews = null;
		List siloEntryMovementsDTOs = null;
		SiloDAO siloDAO = null;
		SiloMovementsConverter siloMovementsConverter = null;
		SiloEntryMovementsDTO siloEntryMovementsDTO = null;
		SiloEntityMovementsView siloEntryMovementsView = null;
		Calendar dateCalendar=null;
		try{
			dateCalendar = Calendar.getInstance();
			dateCalendar.setTimeInMillis(TimeUtils.convertStringToSQLDate(siloFilterView.getDate()).getTime());	
			siloMovementsConverter = new SiloMovementsConverter();
			siloDAO = new SiloDAO();
			siloEntryMovementsDTOs = siloDAO.getEntriesByDay(dateCalendar);
			siloEntryMovementsViews=  new ArrayList(siloEntryMovementsDTOs.size());
			Iterator siloEntryMovementsIteratorDTOs= siloEntryMovementsDTOs.iterator();
			
			while( siloEntryMovementsIteratorDTOs.hasNext() ){
				siloEntryMovementsDTO = (SiloEntryMovementsDTO)siloEntryMovementsIteratorDTOs.next();
				siloEntryMovementsView= siloMovementsConverter.convertEntryDTOToView(siloEntryMovementsDTO);
				siloEntryMovementsViews.add(siloEntryMovementsView);
			}
			
		}catch(SiloException siloException){
			throw siloException;
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			siloException = new SiloException(exception, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloEntryMovementsViews;
	}
	
	public List getOutputs(SiloFiltersView siloFiltersView)throws SiloException{
		List siloOutputViews = null;
		SiloDAO siloDAO = null;
		List siloOutputDTOs = null;
		SiloConverter siloConverter = null;
		SiloFiltersDTO siloFiltersDTO = null;
		Iterator iterator = null;
		SiloOutputDTO siloOutputDTO = null;
		SiloOutputView siloOutputView = null;
		LocationService locationService = null;
		LocationView locationView = null;
		ReasonMovementService reasonMovementService = null;
		ReasonMovementView reasonMovementView = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		try{
			siloDAO = new SiloDAO();
			siloConverter = new SiloConverter();
			locationService = new LocationService();
			reasonMovementService = new ReasonMovementService();
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(OUTPUT_REASON_CODE);
			siloFiltersDTO = siloConverter.convertFiltersViewToDTO(siloFiltersView);
			siloOutputDTOs = siloDAO.getOutputs(siloFiltersDTO);
			siloOutputViews = new ArrayList(siloOutputDTOs.size());
			iterator = siloOutputDTOs.iterator();
			while(iterator.hasNext()){
				siloOutputDTO = (SiloOutputDTO)iterator.next();
				locationView = locationService.getLocation(siloFiltersView.getWarehouseId(), siloOutputDTO.getLocation());
				reasonMovementView = reasonMovementService.getReasonMovement(systemParameterBean.getValue());
				siloOutputView = siloConverter.convertOutputDTOToView(siloOutputDTO);
				siloOutputView.setLocationView(locationView);
				siloOutputView.setReasonMovementView(reasonMovementView);
				siloOutputViews.add(siloOutputView);
			}
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			siloException = new SiloException(exception, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloOutputViews;
	}
	
	public List getEntities(SiloFiltersView siloFiltersView)throws SiloException{
		List siloEntryViews = null;
		SiloDAO siloDAO = null;
		List siloEntryDTOs = null;
		SiloConverter siloConverter = null;
		SiloFiltersDTO siloFiltersDTO = null;
		Iterator iterator = null;
		SiloEntryDTO siloEntryDTO = null;
		SiloEntityView siloEntryView = null;
		UnitService unitService = null;
		LocationService locationService = null;
		LocationView locationView = null;
		List unitViews = null;
		UnitView unitView = null;
		int indexFinWagon = -1;
		try{
			siloDAO = new SiloDAO();
			siloConverter = new SiloConverter();
			unitService = new UnitService();
			locationService = new LocationService();
			unitViews = unitService.getTransits(siloFiltersView.getWarehouseId());
			siloFiltersDTO = siloConverter.convertFiltersViewToDTO(siloFiltersView);
			siloEntryDTOs = siloDAO.getEntries(siloFiltersDTO);
			siloEntryViews = new ArrayList(siloEntryDTOs.size());
			iterator = siloEntryDTOs.iterator();
			while(iterator.hasNext()){
				siloEntryDTO = (SiloEntryDTO)iterator.next();
				locationView = locationService.getLocation(siloFiltersView.getWarehouseId(), siloEntryDTO.getLocation());
				siloEntryView = siloConverter.convertEntryDTOToView(siloEntryDTO);
				siloEntryView.setLocationView(locationView);
				indexFinWagon = unitViews.indexOf(siloEntryView);
				if( indexFinWagon > -1 ){
					unitView = (UnitView)unitViews.get(indexFinWagon);
					siloEntryView.setUnitView(unitView);
					unitViews.remove(indexFinWagon);
				}
				siloEntryViews.add(siloEntryView);
			}
		}catch(UnitException unitException){
			throw new SiloException(unitException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
		}catch(SiloException siloException){
			throw siloException;
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			siloException = new SiloException(exception, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloEntryViews;
	}
}